# backend/app/main.py
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from backend.app.routers import dashboard, requirements, test_cases
from backend.app.database.session import create_tables

app = FastAPI(
    title="AI智能测试平台 API",
    description="AI驱动的需求优化和测试用例生成平台",
    version="1.0.0"
)

# 配置CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # 在生产环境中应该设置为前端域名
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# 注册路由
app.include_router(dashboard.router, prefix="/api", tags=["dashboard"])
app.include_router(requirements.router, prefix="/api", tags=["requirements"])
app.include_router(test_cases.router, prefix="/api", tags=["test_cases"])

@app.on_event("startup")
async def startup():
    # 创建数据库表
    create_tables()

@app.get("/", tags=["root"])
async def root():
    return {"message": "欢迎使用AI智能测试平台 API", "version": "1.0.0"}

if __name__ == "__main__":
    import uvicorn
    uvicorn.run("app.main:app", host="0.0.0.0", port=8000, reload=True)